<template>
  <div style="text-align:center">
    <h1 v-if="downing">下载中</h1>
    <h1 v-if="error">文件获取失败</h1>
  </div>
</template>
<script>
  import fileApi from "@api/modules/system/fileUpload";
  import {downloadBlobFile} from "@/utils/functions";

  export default {
    data() {
      return {
        error: false,
        downing: false
      }
    },
    mounted() {
      this.getFileInfoByIds(this.$route.query.id)
    },
    methods: {
      getFileInfoByIds(id) {
        if (!id) {
          this.$message.error("无效请求")
          return
        }
        fileApi.getFileInfo([id]).then(res => {
          if (!res.data || res.data.length === 0) {
            this.error = true;
            this.$message.error("文件不存在")
            return;
          }
          this.download(res.data[0]);
        }).catch(()=>{
          this.error = true;
          this.$message.error("获取文件信息失败！");
        });
      },
      // 下载附件
      download (file) {
        this.downing = true
        fileApi.downloadFileToLocal({ id: file.id}).then((res) => {
          downloadBlobFile(res, file.name)
          window.close();
        }).finally(() => {
          this.downing = false
        })
      }
    }
  }
</script>